package com.smartwebx.ha.web.gis.module.screen;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.smartwebx.ha.biz.rainwater.feature.FeatureWasManager;
import com.smartwebx.ha.biz.rainwater.real.RiverFloodControlManager;
import com.smartwebx.ha.biz.rainwater.real.WasManager;
import com.smartwebx.ha.dal.annot.JsonModule;
import com.smartwebx.ha.dal.annot.JsonModules;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.RiverFC;
import com.smartwebx.ha.dal.usebean.view.ViewDayProcessAnalysis;
import com.smartwebx.ha.dal.usebean.view.ViewProcessBean;
import com.smartwebx.ha.web.util.json.module.DateModule;
import com.smartwebx.ha.web.util.json.module.DoubleModule;

/**
 * 堰闸 堰闸日水位过程，堰闸时过程线
 * 
 * @author tsing
 * 
 */
public class WeirgatesScreen extends BasicScreen {
	@Autowired
	private WasManager wasManager;
	@Autowired
	private RiverFloodControlManager riverFloodControlManager;
	@Autowired
	private FeatureWasManager featureWasManager;

	/**
	 * 堰闸日水位过程
	 * 
	 * @param stcd
	 *            (51103400)
	 * @param bTm
	 *            (2001-08-09)
	 * @param eTm
	 *            (2002-02-25)
	 * @param dxn
	 *            (2005)
	 * @return
	 * @throws ParseException
	 */
	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
			@JsonModule(clazz = DoubleModule.class, value = "2") })
	public List<ViewProcessBean> getSingleStatDayProcess(String stcd, Date bTm,
			Date eTm, Integer dxn) throws Exception {
		List<DateValue> dateValueWaters = wasManager
				.getSingleStatWaterDayProcess(stcd, bTm, eTm);// 堰闸日水位过程
		RiverFC riverFC = riverFloodControlManager
				.getSingleRiverFloodControl(stcd);// 河道防洪指标
		List<DateValue> dateValueFlows = wasManager
				.getSingleStatFlowDayProcess(stcd, bTm, eTm);// 日流量过程
		// 多年同期
		List<DateValue> dateValueYears = featureWasManager.getPeriodContrast(
				stcd, bTm, eTm);
		// 典型年
		Calendar cal = Calendar.getInstance();
		cal.setTime(bTm);
		int begYear = cal.get(Calendar.YEAR);

		cal.setTime(eTm);
		int endYear = cal.get(Calendar.YEAR);

		int b = dxn - (endYear - begYear);

		Date startDate = DateUtils.setYears(bTm, b);
		Date endDate = DateUtils.setYears(eTm, dxn);
		List<DateValue> dxnDateVlues = wasManager.getSingleStatWaterDayProcess(
				stcd, startDate, endDate);

		List<ViewProcessBean> viewProcessBeans = new ArrayList<ViewProcessBean>();
		for (DateValue dateValueWater : dateValueWaters) {
			ViewProcessBean viewProcessBean = new ViewProcessBean();
			BeanUtils.copyProperties(viewProcessBean, dateValueWater);
			viewProcessBean.setTotalVal(riverFC == null ? 0 : riverFC
					.getWarWater());// 警戒水位
			// 流量过程
			for (DateValue dateValueFlow : dateValueFlows) {
				if (Objects
						.equal(dateValueWater.getTm(), dateValueFlow.getTm())) {
					viewProcessBean.setValue5(dateValueFlow.getVal());
					break;
				}
			}
			// 多年同期
			for (DateValue dateValueYear : dateValueYears) {
				if (Objects.equal(dateValueWater.getTm(),
						DateUtils.addHours(dateValueYear.getTm(), 8))) {
					viewProcessBean.setValue1(dateValueYear.getVal());
					break;
				}
			}
			// 典型年
			for (DateValue dxnDateVlue : dxnDateVlues) {
				String s1 = new SimpleDateFormat("MM-dd").format(dateValueWater
						.getTm());
				String s2 = new SimpleDateFormat("MM-dd").format(dxnDateVlue
						.getTm());
				if (s1.equals(s2)) {
					viewProcessBean.setValue6(dxnDateVlue.getVal());
					break;
				}
			}
			viewProcessBeans.add(viewProcessBean);
		}
		return viewProcessBeans;
	}

	@JsonModule(clazz = DoubleModule.class, value = "2", isNumber = true)
	public Object getSingleStatDayProcessByGisJs(String stcd, Date bTm,
			Date eTm, Integer dxn) throws Exception {
		List<DateValue> dateValueWaters = wasManager
				.getSingleStatWaterDayProcess(stcd, bTm, eTm);// 堰闸日水位过程
		RiverFC riverFC = riverFloodControlManager
				.getSingleRiverFloodControl(stcd);// 河道防洪指标
		List<DateValue> dateValueFlows = wasManager
				.getSingleStatFlowDayProcess(stcd, bTm, eTm);// 日流量过程
		// 多年同期
		List<DateValue> dateValueYears = featureWasManager.getPeriodContrast(
				stcd, bTm, eTm);
		// 典型年
		Calendar cal = Calendar.getInstance();
		cal.setTime(bTm);
		int begYear = cal.get(Calendar.YEAR);

		cal.setTime(eTm);
		int endYear = cal.get(Calendar.YEAR);

		int b = dxn - (endYear - begYear);

		Date startDate = DateUtils.setYears(bTm, b);
		Date endDate = DateUtils.setYears(eTm, dxn);
		List<DateValue> dxnDateVlues = wasManager.getSingleStatWaterDayProcess(
				stcd, startDate, endDate);

		List<Double> vals = Lists.newArrayList();// 水位
		List<Double> totalVals = Lists.newArrayList();// 警戒水位
		List<Double> values5 = Lists.newArrayList();// 流量过程
		List<Double> values1 = Lists.newArrayList();// 多年同期
		List<Double> values6 = Lists.newArrayList();// 典型年
		for (DateValue dateValueWater : dateValueWaters) {
			vals.add(dateValueWater.getVal());
			totalVals.add(riverFC == null ? 0 : riverFC.getWarWater());// 警戒水位
			// 流量过程
			for (DateValue dateValueFlow : dateValueFlows) {
				if (Objects
						.equal(dateValueWater.getTm(), dateValueFlow.getTm())) {
					values5.add(dateValueFlow.getVal());
					break;
				}
			}
			// 多年同期
			for (DateValue dateValueYear : dateValueYears) {
				if (Objects.equal(dateValueWater.getTm(),
						DateUtils.addHours(dateValueYear.getTm(), 8))) {
					values1.add(dateValueYear.getVal());
					break;
				}
			}
			// 典型年
			for (DateValue dxnDateVlue : dxnDateVlues) {
				String s1 = new SimpleDateFormat("MM-dd").format(dateValueWater
						.getTm());
				String s2 = new SimpleDateFormat("MM-dd").format(dxnDateVlue
						.getTm());
				if (s1.equals(s2)) {
					values6.add(dxnDateVlue.getVal());
					break;
				}
			}
		}
		Map<String, List<Double>> result = new HashMap<String, List<Double>>();
		result.put("vals", vals);
		result.put("totalVals", totalVals);
		result.put("values1", values1);
		result.put("values5", values5);
		result.put("values6", values6);
		return result;
	}

	/**
	 * 堰闸小时过程线
	 * 
	 * @param stcd
	 *            (51103400)
	 * @param bTm
	 *            (2000-01-09)
	 * @param eTm
	 *            (2002-02-25)
	 * @return
	 * @throws ParseException
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public List<ViewProcessBean> getSingleStatHourProcess(String stcd,
			Date bTm, Date eTm) throws Exception {
		List<DateValue> dateValueWaters = wasManager.getPurenessWaterProcesss(
				stcd, bTm, eTm);// 堰闸时水位过程
		List<DateValue> dateValueFlows = wasManager.getPurenessFlowProcesss(
				stcd, bTm, eTm);// 堰闸流量小时过程
		List<ViewProcessBean> viewProcessBeans = new ArrayList<ViewProcessBean>();
		for (DateValue dateValueWater : dateValueWaters) {
			ViewProcessBean viewProcessBean = new ViewProcessBean();
			BeanUtils.copyProperties(viewProcessBean, dateValueWater);// / 当前水位
			for (DateValue dateValueFlow : dateValueFlows) {
				if (Objects
						.equal(dateValueWater.getTm(), dateValueFlow.getTm())) {
					viewProcessBean.setValue2(dateValueFlow.getVal());
					break;
				}
			}
			viewProcessBeans.add(viewProcessBean);
		}
		return viewProcessBeans;
	}

	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public Object getSingleStatHourProcessByGisJs(String stcd, Date bTm,
			Date eTm) throws Exception {
		List<DateValue> dateValueWaters = wasManager.getPurenessWaterProcesss(
				stcd, bTm, eTm);// 堰闸时水位过程
		List<DateValue> dateValueFlows = wasManager.getPurenessFlowProcesss(
				stcd, bTm, eTm);// 堰闸流量小时过程
		List<Object[]> vals = Lists.newArrayList();// 水位
		List<Object[]> values2 = Lists.newArrayList();// 流量
		for (DateValue dateValueWater : dateValueWaters) {
			Date dateValTm = dateValueWater.getTm();
			Double dateVal = dateValueWater.getVal();
			Object[] obj = new Object[] { dateValTm.getTime(), dateVal };
			vals.add(obj);// 当前水位

		}
		for (DateValue dateValueFlow : dateValueFlows) {
			Date flowTm = dateValueFlow.getTm();
			Double flowVal = dateValueFlow.getVal();
			if (flowVal != 0) {
				Object[] obj1 = new Object[] { flowTm.getTime(), flowVal };
				values2.add(obj1);
			}
		}
		
		Map<String, List<Object[]>> result = Maps.newHashMap();

		result.put("vals", vals);
		result.put("values2", values2);
		return result;
	}

	/**
	 * 堰闸日过程特征分析,历史最高、最低以及在历史上的排位
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws ParseException
	 */
	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
			@JsonModule(clazz = DoubleModule.class, value = "2") })
	public ViewDayProcessAnalysis getDayProcessAnalysis(String stcd, Date bTm,
			Date eTm) throws ParseException {
		ViewDayProcessAnalysis viewDayProcessAnalysis = wasManager
				.getDayProcessAnalysis(stcd, bTm, eTm);
		return viewDayProcessAnalysis;

	}
}
